package lib.bone.audit.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import lib.bone.access.config.cycle.NepuCycle;
import lib.bone.access.config.cycle.NepuCycleService;
import lib.bone.access.constant.CycleTypeConst;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.constant.WorkloadSourceConst;
import lib.bone.access.constant.WorkloadTypeConst;
import lib.bone.access.event.CycleCreatedEvent;
import lib.bone.access.module.dict.DictServiceCtl;
import lib.bone.access.module.dict.entity.NepuTopicDict;
import lib.bone.audit.workload.attach.ResearchAttach;
import lib.bone.audit.workload.domain.NepuWorkload;
import lib.bone.audit.workload.remark.Remark;
import lib.bone.audit.workload.service.NepuWorkloadService;
import lib.bone.common.utils.JsonUtil;
import lib.bone.nepudata.domain.entity.NepuTopic;
import lib.bone.nepudata.service.impl.NepuTopicServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@Primary
public class CycleCreatedEventImpl implements CycleCreatedEvent {

    @Autowired
    NepuTopicServiceImpl nepuTopicService;

    @Autowired
    DictServiceCtl dictServiceCtl;

    @Autowired
    NepuCycleService nepuCycleService;

    @Autowired
    NepuWorkloadService workloadService;


    @Override
    public void process(NepuCycle nepuCycle) {
        System.out.println("触发事件");
        if (nepuCycle.getCycleType().equals(CycleTypeConst.RESEARCH)) {
            System.out.println("周期类型符合");
            //新建存储工作量数据的数组
            List<NepuWorkload> nepuWorkloads = new ArrayList<>();
            //获取上周期全部的纵向课题
            NepuTopic nepuTopic = new NepuTopic();
            //nepuTopic.setCycleId("40288186801b7d8101801b7fabb50000");
            Example<NepuTopic> example = Example.of(nepuTopic);
            List<NepuTopic> topics = nepuTopicService.getAll(example);
            for (NepuTopic topic : topics) {
                //判断项目的时间段和周期是否有重合时间
                if (topic.getEndDate().compareTo(nepuCycle.getStartDate()) == -1 ||
                        topic.getEndDate().compareTo(nepuCycle.getStartDate()) == 0) {
                    continue;
                }
                if (topic.getStartDate().compareTo(nepuCycle.getEndDate()) == 1 ||
                        topic.getStartDate().compareTo(nepuCycle.getEndDate()) == 0) {
                    continue;
                }
                List<NepuWorkload> list = workloadService.findByLinkId(topic.getId());
                if (list.size()==0){
                    continue;
                }
                //有重合时间，在工作量表中新建
                Date startDate;
                Date endDate;
                if (topic.getStartDate().compareTo(nepuCycle.getStartDate()) == -1 ||
                        topic.getStartDate().compareTo(nepuCycle.getStartDate()) == 0) {
                    startDate = nepuCycle.getStartDate();
                } else {
                    startDate = topic.getStartDate();
                }
                if (topic.getEndDate().compareTo(nepuCycle.getEndDate()) == -1 ||
                        topic.getEndDate().compareTo(nepuCycle.getEndDate()) == 0) {
                    endDate = topic.getEndDate();
                } else {
                    endDate = nepuCycle.getEndDate();
                }
                NepuWorkload nepuWorkload = new NepuWorkload();
                // 设置工作量来源 -- 申请
                nepuWorkload.setWorkloadSource(WorkloadSourceConst.APPLY);
                // 设置工作量类型 -- 科研工作量
                nepuWorkload.setWorkloadType(WorkloadTypeConst.RESEARCH);
                // 设置获取工作量用户ID
                Long userId = topic.getLeaderId();
                nepuWorkload.setUserId(userId);
                //获取科研项目Id
                nepuWorkload.setLinkId(topic.getId());
                //设置备注信息
                nepuWorkload.setRemark("["+nepuCycle.getCycleName()+"]"+topic.getTopicName());
                //设置工作量可分配
                nepuWorkload.setWorkloadLock(2);
                // 获取工作量所在周期 -- 从科研项目中提取
                nepuWorkload.setCycleId(nepuCycle.getId());
                nepuWorkload.setParentId(nepuCycle.getId());
                Optional<Object> objectOp = dictServiceCtl.getById(topic.getTopicDictId(), ResearchTypeConst.TOPIC);
                NepuTopicDict nepuTopicDict = new NepuTopicDict();
                if (objectOp.isPresent()) {
                    BeanUtil.copyProperties(objectOp.get(), nepuTopicDict, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
                    // 设置工作量级别
                    String level = nepuTopicDict.getTopicLevel();
                    nepuWorkload.setWorkloadLevel(level);
                    //工作量计算
                    long betweenMonth = DateUtil.betweenMonth(startDate, endDate, false) + 1;
                    Double workload = nepuTopicDict.getWorkloadMax() * betweenMonth * 1.0 / 12;
                    nepuWorkload.setWorkload(workload);
                    // 设置使用工作量为 0 用尽标识为 0
                    nepuWorkload.setUsedWorkload(0.0);
                    nepuWorkload.setUsedUp(0);
                    // 设置工作量附加信息
                    String attach = buildAttach(topic, nepuTopicDict);
                    nepuWorkload.setWorkloadAttach(attach);
                    // 设置锁等级为 2
                    nepuWorkload.setWorkloadLock(2);
                    nepuWorkloads.add(nepuWorkload);
                }
            }
            workloadService.saveList(nepuWorkloads);
        }
    }

    public String buildAttach(NepuTopic topic, NepuTopicDict nepuTopicDict) {
        ResearchAttach attach = new ResearchAttach();
        attach.setResearchId(topic.getId());
        attach.setResearchName(topic.getTopicName());
        attach.setDictId(nepuTopicDict.getId());
        attach.setDictName(nepuTopicDict.getTopicType());
        attach.setResearchLevel(nepuTopicDict.getTopicLevel());
        attach.setResearchType(ResearchTypeConst.TOPIC);
        return JsonUtil.obj2String(attach);
    }

//    public String buildRemark(NepuTopic nepuTopic,NepuCycle nepuCycle) {
//        Remark remark = new Remark();
//        remark.setTopicName(nepuTopic.getTopicName());
//        remark.setCycleName(nepuCycle.getCycleName());
//        return JsonUtil.obj2String(remark);
//    }

}

